package com.cn.studemo.demos.web.service.Impl;


import com.cn.studemo.demos.web.mapper.UserMapper;
import com.cn.studemo.demos.web.pojo.User;
import com.cn.studemo.demos.web.service.UserService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * TODO
 *
 * @ClassName UserServiceImpl
 * @Author xiaojiang
 * @Date 2021/12/23 下午 16:02
 */
@Service
public class UserServiceImpl implements UserService {

    private static final Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);
    @Resource
    UserMapper userMapper;
    @Autowired
    SqlSessionFactory sqlSessionFactory;
    private static final int BATCH_SIZE = 3000;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addUserOneByOne(List<User> users) {
        SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);
        long startTime = 0;
        try {
            UserMapper um = session.getMapper(UserMapper.class);
            startTime = System.currentTimeMillis();
            logger.info("开始时间 : {}", startTime);
            List<User> userlist = new ArrayList<>();
            for (int i = 1; i < users.size(); i++) {
                userlist.add(users.get(i));
                if ((i % BATCH_SIZE == 0) ) {
//                    System.out.println(userlist.size());
                    um.addByOneSQL(userlist);
                    userlist.clear();
                }
            }
            um.addByOneSQL(userlist);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            session.commit();
            session.close();
        }

        long endTime = System.currentTimeMillis();
        logger.info("结束时间 : {}", startTime);
        logger.info("BATCH_SIZE {}, 一条条插入 SQL 耗费时间 {}", BATCH_SIZE, (endTime - startTime));// 一条条插入 SQL 耗费时间 77719


        /*long startTime = System.currentTimeMillis();
        logger.info("开始时间 = " + startTime);
//        List<User> userlist = new ArrayList<>();
        for (int i = 0; i < users.size(); i++) {
            userMapper.addUserOneByOne(users.get(i));
        }
//        userMapper.addByOneSQL(userlist);
        long endTime = System.currentTimeMillis();
        logger.info("结束时间 = " + endTime);   //     12540   10978
        logger.info("插入耗费时间 {}", (endTime - startTime));//合并成一条 SQL 插入耗费时间 47291
        //  12540  12363    5356
        //  10978 10026*/


        /*SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH);
        UserMapper um = session.getMapper(UserMapper.class);
        long startTime = System.currentTimeMillis();
        logger.info("开始时间 : {}", startTime);
        for (User user : users) {
            um.addUserOneByOne(user);
        }
        session.commit();
        long endTime = System.currentTimeMillis();
        logger.info("结束时间 : {}", startTime);
        logger.info("一条条插入 SQL 耗费时间 {}", (endTime - startTime));*/

    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addByOneSQL(List<User> users) {
        long startTime = System.currentTimeMillis();
        logger.info("开始时间 = " + startTime);
        List<User> userlist = new ArrayList<>();
        for (int i = 1; i < users.size(); i++) {
            userlist.add(users.get(i));
            if ((i % BATCH_SIZE == 0)) {
//                System.out.println(userlist.size());
                userMapper.addByOneSQL(userlist);
                userlist.clear();
            }
        }
        userMapper.addByOneSQL(userlist);
        long endTime = System.currentTimeMillis();
        logger.info("结束时间 = " + endTime);   //     12540   10978
        logger.info("BATCH_SIZE {}, 合并成一条 SQL 插入耗费时间 {}", BATCH_SIZE,  (endTime - startTime));//合并成一条 SQL 插入耗费时间 47291


    }
    public static void main(String[] args) {
        // 十六进制字符串
        String hexString = "c3";

        // 转换为十进制整数
        long decimalValue = hexToDecimal(hexString);

        // 输出结果
        System.out.println("Decimal value: " + decimalValue);
    }

    public static long hexToDecimal(String hexString) {
        // 使用parseInt方法将十六进制字符串转换为十进制整数
        // 第二个参数16表示十六进制
        return Long.parseLong(hexString, 16);
    }




}
